package sjtu.ist.warehouse4ws.action;

import java.io.IOException;
import java.io.StringBufferInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.ServletActionContext;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;


import sjtu.ist.warehouse4ws.epc2bpmn.GenerateBpmn;
import sjtu.ist.warehouse4ws.epc2bpmn.ParserEpc;
import sjtu.ist.warehouse4ws.jcr.RepositoryMoudl;
import sjtu.ist.warehouse4ws.model.Epcunits;
import sjtu.ist.warehouse4ws.tool.GenerateBpel;
import sjtu.ist.warehouse4ws.tool.MyUtils;

import com.opensymphony.xwork2.ActionSupport;

public class GenerateBpelAction extends ActionSupport{
	private GenerateBpel generateBpel=new GenerateBpel();
	private String nodeName;
	private RepositoryMoudl repositoryMoudlService;
    
	
	public RepositoryMoudl getRepositoryMoudlService() {
		return repositoryMoudlService;
	}

	public void setRepositoryMoudlService(RepositoryMoudl repositoryMoudlService) {
		this.repositoryMoudlService = repositoryMoudlService;
	}

	public String getNodeName() {
		return nodeName;
	}
	public void setNodeName(String nodeName) {
		this.nodeName = nodeName;
	}
	
	public GenerateBpel getGenerateBpel() {
		return generateBpel;
	}
	public void setGenerateBpel(GenerateBpel generateBpel) {
		this.generateBpel = generateBpel;
	}
	public String generateBpel(){

		MyUtils.mkDirectory(ServletActionContext.getServletContext().getRealPath("/upload/files/bpel"));
		
		String outpath=ServletActionContext.getServletContext().getRealPath("/upload/files/bpel/").replaceAll("\\\\", "/")+"/";


		Subject currentUser = SecurityUtils.getSubject();
		String companyId=currentUser.getSession().getAttribute("companyId").toString();	

		HttpServletRequest request = ServletActionContext.getRequest();
		String xmlName = request.getParameter("nodeName");
		String xmlVersion=request.getParameter("nodeVersion");
	   
		
		List<String> addFile=new ArrayList<String>();
	    HashMap <String,String>fs=this.repositoryMoudlService.getAllFile(companyId, xmlName, xmlVersion,"process");
			generateBpel.setFs(fs);
		    Iterator it = fs.keySet().iterator();
			while (it.hasNext()) {
				
				String key = (String) it.next();
				String outFile=outpath+"BPEL_"+key;
			
				try {
					
					
					Epcunits e = ParserEpc.parseByIS(new StringBufferInputStream(fs.get(key)));
					
					Document doc = generateBpel.polishBPELModelsByBreadthFirst(e.getEpcUnits());
					GenerateBpmn.domDocToFile(doc, outFile, "UTF-8");
					addFile.add(outFile);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}		
					
				
				
		
					
			}
		
			this.repositoryMoudlService.addServiceFile(companyId, xmlName, xmlVersion, "bpel", addFile);
		return SUCCESS;
	}

}
